package app.controllers.mobileweb;

import app.dtos.Shake;
import app.interceptors.MobileWxMemberInterceptor;
import com.alibaba.fastjson.JSON;
import com.github.sog.controller.AjaxMessage;
import com.github.sog.controller.BasicController;
import com.jfinal.aop.Before;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *     摇一摇
 * </p>
 *
 * @author Jerry Ou
 * @version 1.0 2014-04-27 13:04
 * @since JDK 1.6
 */
@Before(MobileWxMemberInterceptor.class)
public class ShakeController extends BasicController {

    /**
     * 摇一摇页面
     */
    public void index() {
        setAttr("phone", getPara("phone"));
        render("index.ftl");
    }

    public void list() {
        List<Map.Entry<String,Integer>> list = Shake.me().list(10);
        renderJson(AjaxMessage.ok("请求成功", JSON.toJSON(list)));
    }

    public void start() {
        Shake.me().start();
        renderJson(AjaxMessage.ok("启动成功"));
    }

    public void stop() {
        Shake.me().stop();
        renderJson(AjaxMessage.ok("停止成功"));
    }

    public void shake() {
        if (Shake.me().started) {
            String phone = getPara("phone");
            Shake.me().shake(phone);
            renderJson(AjaxMessage.ok("摇一摇成功"));
        } else {
            renderJson(AjaxMessage.ok("摇一摇还没开始"));
        }
    }

}
